package generator.service.impl;

import com.alibaba.excel.EasyExcel;
import com.atkt.ktkt.model.vod.Subject;
import com.atkt.ktkt.vo.vod.SubjectEeVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import generator.listener.SubjectListener;
import generator.service.SubjectService;
import generator.mapper.SubjectMapper;
import ktkt.exception.GgktException;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;

/**
* @author 罗雅文
* @description 针对表【subject(课程科目)】的数据库操作Service实现
* @createDate 2022-09-18 09:38:08
*/
@Service
public class SubjectServiceImpl extends ServiceImpl<SubjectMapper, Subject>
    implements SubjectService{

    @Override
    public List<Subject> selectSubjectList(Long id) {
        //select * from SUBJECT where parent_id=0
        LambdaQueryWrapper<Subject> Wrapper = new LambdaQueryWrapper<>();
        Wrapper.eq(Subject::getParentId,id);
        List<Subject> subjects = baseMapper.selectList(Wrapper);

        for (Subject subject : subjects) {
            //获取subject 的id值
            Long subjectId = subject.getId();
            //查询
            boolean isChild= this.isChilder(subjectId);

            subject.setHasChildren(isChild);
        }

        return subjects;
    }

    @Override
    public void exportData(HttpServletResponse response) {
        try {
            response.setContentType("application/vnd.ms-excel");
            response.setCharacterEncoding("utf-8");
            // 这里URLEncoder.encode可以防止中文乱码 当然和easyexcel没有关系
            String fileName = URLEncoder.encode("课程分类", "UTF-8");
            response.setHeader("Content-disposition", "attachment;filename="+ fileName + ".xlsx");

            //查询课程分类表所有数据
            List<Subject> dictList = baseMapper.selectList(null);
            List<SubjectEeVo> dictVoList = new ArrayList<>(dictList.size());
            for(Subject dict : dictList) {
                SubjectEeVo dictVo = new SubjectEeVo();
                BeanUtils.copyProperties(dict,dictVo);
                dictVoList.add(dictVo);
            }

            //EasyExcel 写操作
            EasyExcel.write(response.getOutputStream(), SubjectEeVo.class).sheet("课程分类").doWrite(dictVoList);
        } catch (Exception e) {
            throw new GgktException(20001,"导出失败");
        }
    }

    @Autowired
    private SubjectListener subjectListener;

    //课程分类导入
    @Override
    public void importData(MultipartFile file) {
        try {
            EasyExcel.read(file.getInputStream(),SubjectEeVo.class,subjectListener).sheet().doRead();
        } catch (IOException e) {
            throw new GgktException(20001,"导入失败");
        }
    }

    //判断是否有下一次数据
    private boolean isChilder(Long subjectId) {
        LambdaQueryWrapper<Subject> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Subject::getParentId,subjectId);
        Integer count = baseMapper.selectCount(wrapper);

        return  count>0;

    }
}




